#!/bin/sh
if [ $# = 0 ]; then
  echo "Usage: $0 <file to translate>"
  exit 0
fi

SOURCE=$1
SUFFIX=`echo ${SOURCE} | ${SED:-sed} 's/.*\(\.[^.]*\)$/\1/'`

case ${SOURCE} in
  *.idl)
    TARGET=`basename ${SOURCE} ${SUFFIX}`${LABEL}.texi
    SEDFILE=idl.sed;;
  *.c)
    SEDFILE=c.sed ;
    TARGET=`basename ${SOURCE} ${SUFFIX}`${LABEL}.c.texi
    ;;
  *.h)
    TARGET=`basename ${SOURCE} ${SUFFIX}`${LABEL}.h.texi ;
    SEDFILE=c.sed;;
  *.aadl)
    TARGET=`basename ${SOURCE} ${SUFFIX}`${LABEL}.texi ;
    SEDFILE=aadl.sed;;
  *)
    echo "Cannot translate file";
    exit 1;;
esac

TMPTAR=${TARGET}.tmp

${AWK:-awk} 'BEGIN{out=1}$1=="end"&&substr($0,1,1)=="e"{out=1}out==1{print}$1=="private"&&out==1{out=0; print "   --  implementation removed"}' ${SOURCE} > genout

echo "@smallexample"      >${TMPTAR}
if [ $# = 1 ]; then
echo "@cartouche"        >>${TMPTAR}
echo "@group"            >>${TMPTAR}
fi
echo ""                  >>${TMPTAR}
${SED:-sed} -f ${SEDFILE} genout    >>${TMPTAR}
echo ""                  >>${TMPTAR}
if [ $# = 1 ]; then
echo "@end group"        >>${TMPTAR}
echo "@end cartouche"    >>${TMPTAR}
fi
echo "@end smallexample" >>${TMPTAR}

cat ${TMPTAR} | tr -d '\r' > ${TARGET}
rm ${TMPTAR}
